package cn.iocoder.yudao.module.resource.controller.admin.material;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.material.vo.ResourceMaterialPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.material.vo.ResourceMaterialRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.material.vo.ResourceMaterialSaveReqVO;
import cn.iocoder.yudao.module.resource.convert.ResourceMaterialConvert;
import cn.iocoder.yudao.module.resource.dal.dataobject.material.ResourceMaterialDO;
import cn.iocoder.yudao.module.resource.service.material.ResourceMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 资源素材 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 资源素材")
@RestController
@RequestMapping("/resource/material")
@Validated
public class ResourceMaterialController {

    @Resource
    private ResourceMaterialService materialService;

    @PostMapping("/create")
    @Operation(summary = "创建资源素材")
    @PreAuthorize("@ss.hasPermission('resource:material:create')")
    public CommonResult<Long> createMaterial(@Valid @RequestBody ResourceMaterialSaveReqVO createReqVO) {
        return success(materialService.createMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源素材")
    @PreAuthorize("@ss.hasPermission('resource:material:update')")
    public CommonResult<Boolean> updateMaterial(@Valid @RequestBody ResourceMaterialSaveReqVO updateReqVO) {
        materialService.updateMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源素材")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:material:delete')")
    public CommonResult<Boolean> deleteMaterial(@RequestParam("id") Long id) {
        materialService.deleteMaterial(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除资源素材")
    @Parameter(name = "ids", description = "编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('resource:material:delete')")
    public CommonResult<Boolean> deleteMaterialList(@RequestParam("ids") List<Long> ids) {
        materialService.deleteMaterialList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源素材详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:material:query')")
    public CommonResult<ResourceMaterialRespVO> getMaterial(@RequestParam("id") Long id) {
        ResourceMaterialDO material = materialService.getMaterial(id);
        return success(ResourceMaterialConvert.INSTANCE.convert(material));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源素材分页")
    @PreAuthorize("@ss.hasPermission('resource:material:query')")
    public CommonResult<PageResult<ResourceMaterialRespVO>> getMaterialPage(@Valid ResourceMaterialPageReqVO pageReqVO) {
        PageResult<ResourceMaterialDO> pageResult = materialService.getMaterialPage(pageReqVO);
        return success(ResourceMaterialConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源素材 Excel")
    @PreAuthorize("@ss.hasPermission('resource:material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialExcel(@Valid ResourceMaterialPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceMaterialDO> list = materialService.getMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源素材.xls", "数据", ResourceMaterialRespVO.class,
                BeanUtils.toBean(list, ResourceMaterialRespVO.class));
    }

}
